# Apply new tag rule to all the messages in gidx
# Rewrite a new gidx

import hall
import autotagger
from bsddb import db
import sys
import cPickle
import util

if __name__ == '__main__':

    autoTagger = autotagger.AutoTagger(hall.filterconf)    
    
    src = db.DB()
    src.open(sys.argv[1], dbtype = db.DB_RECNO, flags = db.DB_CREATE)
    dst = db.DB()
    dst.open(sys.argv[2], dbtype = db.DB_RECNO, flags = db.DB_CREATE)

    cur = src.cursor()

    data = cur.first()
    while (data):
        recno, idxdata = data

        threadID, tags, payloadHeader, mailHeaders = cPickle.loads(idxdata)
        tagPool = {}
        headerPool = util.buildHeaderDict(mailHeaders)        
        autoTagger.tag (headerPool, tagPool)

        newIdxData = cPickle.dumps((threadID, tagPool.keys(),
                                    payloadHeader, mailHeaders))

        dst.append(newIdxData)
        data = cur.next()        

    src.close()
    dst.close()

